﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Test2.Models;

namespace Test2
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "";
            do
            {
                var switchVal = -1;
                try{
                    Console.WriteLine("1: Visa alla hoppare m hopp, 2: Lägg till hoppare, 3: Ändra namn på hoppare, 4. Visa alla hoppar utan hopp, 5. Ta bort hoppare");
                    Console.Write("Ange val: ");

                    input = Console.ReadLine();
                    switchVal = Convert.ToInt32(input);
                }
                catch
                {
                    switchVal = -1;
                    input = "";
                }
                switch(switchVal)
                {
                    case 1:
                        PrintAllNames(true);
                        break;
                    case 2:
                        AddNewDiver();
                        break;
                    case 3:
                        EditName();
                        break;
                    case 4:
                        PrintAllNames();
                        break;
                    case 5:
                        RemoveDiver();
                        break;
                    default:
                        break;
                }
                Console.WriteLine();
            } while (!input.Equals(""));
            Console.WriteLine("Program ended");
            Console.ReadKey();
        }

        private static void RemoveDiver()
        {
            using (var db = new DivingContext())
            {
                PrintAllNames();
                Console.WriteLine();
                Console.WriteLine("Remove diver:");
                var nameToBeFound = ReadDiverName();
                var person = (from b in db.Divers
                              where b.Name.ToLower().Equals(nameToBeFound.ToLower())
                              select b).FirstOrDefault();
                if (person != null)
                {
                    db.Divers.Remove(person);
                    db.SaveChanges();
                    Console.WriteLine("Person removed");
                }
                else
                {
                    Console.WriteLine("Person not found");
                }
            }
            
        }

        private static void PrintAllNames(bool printAllJumps = false)
        {
            using (var db = new DivingContext())
            {
                var selectQuery = from b in db.Divers
                                  orderby b.Name
                                  select b;
                Console.WriteLine("All persons");
                foreach (var person in selectQuery)
                {
                    Console.WriteLine(person.Id + ", " + person.Name);
                    if (printAllJumps)
                    {
                        PrintAllJumps(person);
                        Console.WriteLine();
                    }
                }
            }
        }

        private static void PrintAllJumps(Diver person)
        {
            foreach (var jump in person.Jumps)
                Console.WriteLine("Jump:" + jump.Height + ", " + jump.DiveType.ToString());
        }

        private static Diver GetDiverFromNameInput()
        {
            using (var db = new DivingContext())
            {
                var nameToBeFound = ReadDiverName();
                return (from b in db.Divers
                                 where b.Name.ToLower().Equals(nameToBeFound.ToLower())
                                 select b).FirstOrDefault();
                
            }


        }

        private static void EditName()
        {
            using (var db = new DivingContext())
            {
                PrintAllNames();
                Console.WriteLine();
                var nameToBeFound = ReadDiverName();
                var person = (from b in db.Divers
                        where b.Name.ToLower().Equals(nameToBeFound.ToLower())
                        select b).FirstOrDefault();
                if (person != null)
                {
                    person.Name = ReadDiverName();
                    db.SaveChanges();
                }
                else
                {
                    Console.WriteLine("Person not found");
                }
            }
        }

        private static void AddNewDiver()
        {
            using (var db = new DivingContext())
            {
                int HashCode = CreateHash();
                var name = ReadDiverName();

                Diver newDiver = new Diver
                {
                    Name = name,
                    Id = HashCode
                };
                string height = "";
                do
                {
                    Console.Write("Adding jump, enter height(Enter to exit): ");
                    height = Console.ReadLine();

                    try
                    {
                        var intHeight = Convert.ToInt32(height);
                        

                        Console.Write("enter difficulty height(Enter to exit): ");
                        var difficulty = Console.ReadLine();
                        var intDifficulty = Convert.ToInt32(difficulty);

                        newDiver.AddJump(intHeight, intDifficulty);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Moving on...");
                        height = "";
                    }
                } while (!height.Equals(""));

                db.Divers.Add(newDiver);
                db.SaveChanges();
            }
        }

        private static string ReadDiverName()
        {
            Console.Write("Enter diver name: ");
            return Console.ReadLine();
            
        }

        private static int CreateHash()
        {
            int HashCode = DateTime.Now.ToLongTimeString().GetHashCode() % 3000;
            if (HashCode < 0)
                HashCode = -HashCode;
            return HashCode;
        }
    }
}
